package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.StudentEvaluationScoreRecordJdbc;
import com.wuseguang.springcloud.dao.repository.StudentEvaluationScoreRecordRepository;
import com.wuseguang.springcloud.service.StudentEvaluationScoreRecordService;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学生评价分数记录 表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-04-22
 */
@Service
public class StudentEvaluationScoreRecordServiceImpl implements StudentEvaluationScoreRecordService {

    @Autowired
    private StudentEvaluationScoreRecordJdbc studentEvaluationScoreRecordJdbc;
    @Autowired
    private StudentEvaluationScoreRecordRepository studentEvaluationScoreRecordRepository;

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public Result findClassTeacher(Integer classId) {

        String sql ="select tr.id,teacher_id,s.name from teacher_role tr left join roles s on(s.id=tr.role_id) where tr.type = 3 and tr.pid = ?";

        List<Map<String,Object>> list =  studentEvaluationScoreRecordJdbc.queryListMap(sql,classId);
        return new Result(list);
    }

    @Override
    public Result findPersonalPerformance(Integer studentId, Integer findType, String startTime, String endTime, Integer teacherId) {
        //如果查询等于自定义,并且开始时间结束时间有一个为空就不查询
        if (findType == 3 && StringUtils.isEmpty(startTime)
                || findType == 3 && StringUtils.isEmpty(endTime)) {
            return new Result();
        }

        String sql = " from ( " +
                " select sesr.id,sesr.fraction,sesr.content,sesr.name,CONCAT('由',t.`name`,'点评了',s.student_name,'同学') commentTime,sesr.appraiser_id,DATE_FORMAT(sesr.add_time,'%Y-%m-%d %T') add_time ,if(SIGN(sesr.fraction)=-1,0,1) type,si.icon from student_evaluation_score_record sesr LEFT JOIN teacher t on(t.id=sesr.appraiser_id) LEFT JOIN  student s on(sesr.type_id=s.id) left join score_item si on(si.id=sesr.score_item_id) where sesr.type = 0 and s.id=? and sesr.appraiser=0 " +
                " UNION ALL " +
                " select sesr.id,sesr.fraction,sesr.content,sesr.name,CONCAT('由',t.`name`,'点评了',cg.name) commentTime,sesr.appraiser_id,DATE_FORMAT(sesr.add_time,'%Y-%m-%d %T') add_time,if(SIGN(sesr.fraction)=-1,0,1) type,si.icon from student_evaluation_score_record sesr LEFT JOIN teacher t on(t.id=sesr.appraiser_id) LEFT JOIN group_student gs on(sesr.type_id=gs.group_id) LEFT JOIN class_group cg on(gs.group_id=cg.id) left join score_item si on(si.id=sesr.score_item_id)  where sesr.type = 2 and gs.student_id=? and sesr.appraiser=0 " +
                " UNION ALL " +
                " select sesr.id,sesr.fraction,sesr.content,sesr.name,CONCAT('由',t.`name`,'点评了全班') commentTime,sesr.appraiser_id,DATE_FORMAT(sesr.add_time,'%Y-%m-%d %T') add_time,if(SIGN(sesr.fraction)=-1,0,1) type,si.icon from student_evaluation_score_record sesr LEFT JOIN teacher t on(t.id=sesr.appraiser_id)  LEFT JOIN  student s on(sesr.type_id=s.class_id) left join score_item si on(si.id=sesr.score_item_id)  where sesr.type = 1 and s.id=? and sesr.appraiser=0 " +
                ") a where 0 = 0 ";


        //查询文字点评数据
        String sqlThree = "select id,content,class_id,teacher_id,teacher_name,student_id,DATE_FORMAT(add_time,'%Y-%m-%d %T') add_time,CONCAT('https://oss.yuguozhijia.online/systemicon/%E6%96%87%E5%AD%97%E7%82%B9%E8%AF%84.png') icon from text_evaluation where student_id = ? ";

        //判断查询类型
        if (findType == 1) {
            logger.info("查询上周数据");
            sql += " and YEARWEEK(date_format(add_time,'%Y-%m-%d')) = YEARWEEK(now())-1 ";
            sqlThree += " and YEARWEEK(date_format(add_time,'%Y-%m-%d')) = YEARWEEK(now())-1 ";
        } else if (findType == 2) {
            logger.info("查询本月数据");
            sql += " and date_format(add_time,'%Y-%m')=date_format(now(),'%Y-%m') ";
            sqlThree += " and date_format(add_time,'%Y-%m')=date_format(now(),'%Y-%m') ";
        } else if (findType == 3) {
            logger.info("查询自定义数据");
            sql += " and date_format(add_time,'%Y-%m-%d')  between date_format('" + startTime + "','%Y-%m-%d') and date_format('" + endTime + "','%Y-%m-%d') ";
            sqlThree += " and date_format(add_time,'%Y-%m-%d')  between date_format('" + startTime + "','%Y-%m-%d') and date_format('" + endTime + "','%Y-%m-%d') ";
        } else {
            logger.info("查询本周数据");
            sql += " and YEARWEEK(date_format(add_time,'%Y-%m-%d')) = YEARWEEK(now()) ";
            sqlThree += " and YEARWEEK(date_format(add_time,'%Y-%m-%d')) = YEARWEEK(now()) ";
        }

        //判断根据教师查询不为空
        if(!StringUtils.isEmpty(teacherId)){
            sql += " and appraiser_id  ="+teacherId;
            sqlThree += " and teacher_id =  "+teacherId;
        }

        //重新拼接sql
        String sqlOne = "select * " + sql+" order by id desc ";
        //查询得分情况
        String sqlTwo = "select sum(fraction) fractionSum  " + sql + " GROUP BY sign(fraction) ORDER BY fractionSum desc";

        sqlThree += " order by id desc ";

        //查班级表现详情
        List<Map<String, Object>> list = studentEvaluationScoreRecordJdbc.queryListMap(sqlOne, studentId, studentId, studentId);
        //查询加减分
        List<Map<String, Object>> fractionList = studentEvaluationScoreRecordJdbc.queryListMap(sqlTwo, studentId, studentId, studentId);
        //查询文字点评
        List<Map<String, Object>> textList = studentEvaluationScoreRecordJdbc.queryListMap(sqlThree, studentId);

        if(fractionList.isEmpty()){
            //应为这个学生没有分数，所以要填写默认分数
            Map<String,Object> map1 = new HashMap<>();
            map1.put("fractionSum",0);
            Map<String,Object> map2 = new HashMap<>();
            map2.put("fractionSum",0);
            fractionList.add(map1);
            fractionList.add(map2);
        }else if(fractionList.size()==1){
            //判断正负数
            if(Integer.valueOf(fractionList.get(0).get("fractionSum").toString())>0){
                Map<String,Object> map2 = new HashMap<>();
                map2.put("fractionSum",0);
                fractionList.add(map2);
            }else{
                //获取负分的分数
                Integer  fractionSum =  Integer.valueOf(fractionList.get(0).get("fractionSum").toString());
                //重新创建
                fractionList = new ArrayList<>();
                Map<String,Object> map1 = new HashMap<>();
                map1.put("fractionSum",0);
                Map<String,Object> map2 = new HashMap<>();
                map2.put("fractionSum",fractionSum);
                //赋值
                fractionList.add(map1);
                fractionList.add(map2);
            }
        }

        //返回结果
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("fractionList", fractionList);
        map.put("textList", textList);
        return new Result(map);
    }


}
